package selfview.sufutian.com.myapplication.draw2d;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

import java.util.ArrayList;
import java.util.Random;

import selfview.sufutian.com.myapplication.R;

import static android.view.Gravity.BOTTOM;
import static android.view.Gravity.LEFT;
import static android.view.Gravity.RIGHT;
import static android.view.Gravity.TOP;

/**
 * Created by sufutian on 2017/2/6.
 */

public class StarDraw extends View {

    private Bitmap mBackBitmap;
    private int mBackWidth;
    private int mBackHeight;
    private int mStarOneWidth;
    private int mStarOneHeight;
    private int mStarTwoWidth;
    private int mStarTwoHeight;
    private int mStarThreeWidth;
    private int mStarThreeHeight;
    private int mTotalWidth;
    private int mTotalHeight;
    private Bitmap mStarOne;
    private Bitmap mStarTwo;
    private Bitmap mStarThree;
    private Paint mPaint;
    private Rect mStarOneSrcRect;
    private Rect mStarTwoSrcRect;
    private Rect mStarThreeSrcRect;

    public StarDraw(Context context) {
        super(context);
        initBitmapInfo();
        initPaint();
        initStarInfo();
    }

    public StarDraw(Context context, AttributeSet attrs) {
        super(context, attrs);
        initBitmapInfo();
        initPaint();
        initStarInfo();
    }

    public StarDraw(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    private void initPaint() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setFilterBitmap(true);
        mPaint.setDither(true);

    }
    /**
     * 2 我们用数组先定义一批星球的位置
     */
    private static final float[][] STAR_LOCATION = new float[][]{
            {0.5f, 0.2f}, {0.68f, 0.35f}, {0.5f, 0.05f},
            {0.15f, 0.15f}, {0.5f, 0.5f}, {0.15f, 0.8f},
            {0.2f, 0.3f}, {0.77f, 0.4f}, {0.75f, 0.5f},
            {0.8f, 0.55f}, {0.9f, 0.6f}, {0.1f, 0.7f},
            {0.1f, 0.1f}, {0.7f, 0.8f}, {0.5f, 0.6f}
    };

    /**
     * 3. 定义三种不同快慢的漂浮速度：
     */
    int mFloatTransLowSpeed = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 0.5f,
            getResources().getDisplayMetrics());
    int mFloatTransMidSpeed = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 0.75f,
            getResources().getDisplayMetrics());
    int mFloatTransFastSpeed = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1f,
            getResources().getDisplayMetrics());


    /**
     * 获取星球大小
     */
    private float getStarSize(float start, float end) {
        float nextFloat = (float) Math.random();
        if (start < nextFloat && nextFloat < end) {
            return nextFloat;
        } else {
            // 如果不处于想要的数据段，则再随机一次，因为不断递归有风险
            return (float) Math.random();
        }

    }

    /**
     * 1 初始化星球
     */
    private void initBitmapInfo() {
        mBackBitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.back)).getBitmap();
        mBackWidth = mBackBitmap.getWidth();
        mBackHeight = mBackBitmap.getHeight();

        mStarOne = ((BitmapDrawable) getResources().getDrawable(R.drawable.start1)).getBitmap();
        mStarOneWidth = mStarOne.getWidth();
        mStarOneHeight = mStarOne.getHeight();
        mStarOneSrcRect = new Rect(0,0,mStarOneWidth,mStarOneHeight);

        mStarTwo = ((BitmapDrawable) getResources().getDrawable(R.drawable.start2)).getBitmap();
        mStarTwoWidth = mStarTwo.getWidth();
        mStarTwoHeight = mStarTwo.getHeight();
        mStarTwoSrcRect = new Rect(0,0,mStarTwoWidth,mStarTwoHeight);


        mStarThree = ((BitmapDrawable) getResources().getDrawable(R.drawable.start3)).getBitmap();
        mStarThreeWidth = mStarThree.getWidth();
        mStarThreeHeight = mStarThree.getHeight();
        mStarThreeSrcRect = new Rect(0,0,mStarTwoWidth,mStarTwoHeight);

    }


    private ArrayList<StarInfo> mStarInfos=new ArrayList<>();

    /**
     * 星球
     */
    private class StarInfo {
        // 缩放比例
        float sizePercent;
        // x位置
        int xLocation;
        // y位置
        int yLocation;
        // 透明度
        float alpha;
        // 漂浮方向
        int direction;
        // 漂浮速度
        int speed;
    }

    /**
     * 4 初始化星球运行方向
     */
    private int getStarDirection() {
        Random random = new Random();
        int randomInt = random.nextInt(4);
        int direction = 0;
        switch (randomInt) {
            case 0:
                direction = LEFT;
                break;
            case 1:
                direction = RIGHT;
                break;
            case 2:
                direction = TOP;
                break;
            case 3:
                direction = BOTTOM;
                break;
            default:
                break;
        }
        return direction;
    }



    private int mStarCount=15;
    /**
     * 初始化星球信息
     */
    private void initStarInfo() {

        StarInfo starInfo = null;
        Random random = new Random();
        for (int i = 0; i < mStarCount; i++) {
            // 获取星球大小比例
            float starSize = getStarSize(0.4f, 0.9f);
            // 初始化星球大小
            float[] starLocation = STAR_LOCATION[i];
            starInfo = new StarInfo();
            starInfo.sizePercent = starSize;

            // 初始化漂浮速度
            int randomSpeed = random.nextInt(3);
            switch (randomSpeed) {
                case 0:
                    starInfo.speed = mFloatTransLowSpeed;
                    break;
                case 1:
                    starInfo.speed = mFloatTransMidSpeed;
                    break;
                case 2:
                    starInfo.speed = mFloatTransFastSpeed;
                    break;
                default:
                    starInfo.speed = mFloatTransMidSpeed;
                    break;
            }

            // 初始化星球透明度
            starInfo.alpha = getStarSize(0.3f, 0.8f);
            // 初始化星球位置
            starInfo.xLocation = (int) (starLocation[0] * mTotalWidth);
            starInfo.yLocation = (int) (starLocation[1] * mTotalHeight);
            Log.e("sufutian","xLocation = " + starInfo.xLocation + "--yLocation = "
                    + starInfo.yLocation);
            Log.e("sufutian","stoneSize = " + starSize + "---stoneAlpha = "
                    + starInfo.alpha);
            // 初始化星球位置
            starInfo.direction = getStarDirection();
            mStarInfos.add(starInfo);
        }

    }


    private void drawStarDynamic(int count, StarInfo starInfo,
                                 Canvas canvas, Paint paint) {

        float starAlpha = starInfo.alpha;
        int xLocation = starInfo.xLocation;
        int yLocation = starInfo.yLocation;
        float sizePercent = starInfo.sizePercent;

        xLocation = (int) (xLocation / sizePercent);
        yLocation = (int) (yLocation / sizePercent);

        Bitmap bitmap = null;
        Rect srcRect = null;
        Rect destRect = new Rect();

        if (count % 3 == 0) {

            bitmap = mStarOne;
            srcRect = mStarOneSrcRect;
            destRect.set(xLocation, yLocation,
                    xLocation + mStarOneWidth, yLocation
                            + mStarOneHeight);
        } else if (count % 2 == 0) {
            bitmap = mStarThree;
            srcRect = mStarThreeSrcRect;
            destRect.set(xLocation, yLocation, xLocation
                    + mStarThreeWidth, yLocation + mStarThreeHeight);
        } else {
            bitmap = mStarTwo;
            srcRect = mStarTwoSrcRect;
            destRect.set(xLocation, yLocation, xLocation
                    + mStarTwoWidth, yLocation + mStarTwoHeight);
        }

        paint.setAlpha((int) (starAlpha * 255));
        canvas.save();
        canvas.scale(sizePercent, sizePercent);
        canvas.drawBitmap(bitmap, srcRect, destRect, paint);
        canvas.restore();

    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mTotalWidth = w;
        mTotalHeight = h;
    }


    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);

        for(int i=0;i<mStarInfos.size();i++){
            drawStarDynamic(15,mStarInfos.get(i),canvas,mPaint);
            resetStarFloat(mStarInfos.get(i));
        }
    }

    /**
     * 每次重绘的时候将星球的x、y增加或减小对应大小
     * @param starInfo
     */
    private void resetStarFloat(StarInfo starInfo) {
        switch (starInfo.direction) {
            case LEFT:
                starInfo.xLocation -= starInfo.speed;
                break;
            case RIGHT:
                starInfo.xLocation += starInfo.speed;
                break;
            case TOP:
                starInfo.yLocation -= starInfo.speed;
                break;
            case BOTTOM:
                starInfo.yLocation += starInfo.speed;
                break;
            default:
                break;
        }
    }
}
